package top.i51213.fastaction.action.share;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.view.View;

import androidx.core.content.FileProvider;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import top.i51213.fastaction.BuildConfig;
import top.i51213.fastaction.MainSettingsFragment;
import top.i51213.fastaction.action.BaseActonComponent;
import top.i51213.fastaction.action.Data;
import top.i51213.fastaction.action.DataType;
import top.i51213.fastaction.action.IntentType;
import top.i51213.fastaction.action.SameLast;
import top.i51213.fastaction.util.Util;

public class ShareActonComponent extends BaseActonComponent implements SameLast {
    public ShareActonComponent(Context context) {
        super(context);
    }

    @Override
    public int getImageResId() {
        return android.R.drawable.ic_menu_share;
    }

    @Override
    public String getTitle() {
        return "分享";
    }

    @Override
    public String getInfo() {
        return "";
    }

    @Override
    public View getEditView() {
        return null;
    }

    @Override
    public void editViewDismissListener() {

    }

    @Override
    protected Data makeOutputData() {
        share();
        return null;
    }

    @Override
    public IntentType getCurrentIntentType() {
        return IntentType.Share;
    }

    @Override
    public DataType getOutputDataType() {
        return null;
    }

    private void share() {
        Data data = getInputData();
        ArrayList<Uri> uris = data.getUris();
        filterUri(uris);
        if (uris.size() == 0) return;
        Intent shareIntent = uris.size() > 1 ? new Intent(Intent.ACTION_SEND_MULTIPLE) : new Intent(Intent.ACTION_SEND);
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        shareIntent.addCategory("android.intent.category.DEFAULT");
        shareIntent.setType(getMimeTypeString(data.getDataType()));
        if (uris.size() > 1) {
            shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
        } else {
            shareIntent.putExtra(Intent.EXTRA_STREAM, uris.get(0));
        }
        if(MainSettingsFragment.isUseSelfShare(getContext())){
            shareIntent = new IntentChooser(getContext(), shareIntent)
                    .get(("分享到"), info -> !info.activityInfo.packageName.equals(getContext().getPackageName()));
        }
        try {
            getContext().startActivity(shareIntent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void filterUri(List<Uri> list) {
        for (int i = 0; i < list.size(); i++) {
            Uri uri = list.get(i);
            if ("file".equals(uri.getScheme())) {
                File file = new File(uri.getPath());
                Uri uri2 = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".provider", file, file.getName());
                list.set(i, uri2);
            }
        }
    }

    private static String getMimeTypeString(DataType dataType) {
        switch (dataType) {
            case IMAGE:
                return "image/*";
            case PdfFile:
                return "application/pdf";
            case ZipFile:
                return "application/zip";
            default:
                return "*/*";
        }
    }
}
